package strategy;

/**
 * 1. 策略模式是拿来干什么用的
 *      比如算术，商场打折时，需要不同的策略，我们就可以使用策略模式;
 *      策略模式符合 "开放-封闭" 原则, 需要新增策略时新增一个类即可，不需要修改原有文件
 *
 * 2. 策略模式在 JDK 或技术框架里有明显应用吗
 *
 */
public class Operation{

}

class OperationAdd implements Strategy{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}

class OperationSubtract implements Strategy{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}

class OperationMultiply implements Strategy{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }
}

class OperationDivide implements Strategy{
    @Override
    public int doOperation(int num1, int num2) {
        if (num2 == 0) {
            System.out.println("除数不能为0");
            System.exit(1);
        }
        return num1 / num2;
    }
}

class Context {
    private Strategy strategy;

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2) {
        return strategy.doOperation(num1, num2);
    }
}

class Client {
    public static void main(String[] args) {
        int result = 0;
        Context context = new Context();
        context.setStrategy(new OperationAdd());
        System.out.println("Operation Add 1 + 2 = "+ context.executeStrategy(1, 2));

        context.setStrategy(new OperationSubtract());
        System.out.println("Operation Subtract 5 - 3 = "+ context.executeStrategy(5, 3));

        context.setStrategy(new OperationMultiply());
        System.out.println("Operation Multiply 5 * 3 = "+ context.executeStrategy(5, 3));
    }
}